package com.ysd.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ysd.entity.Books;
import com.ysd.service.BooksService;
import com.ysd.util.Result;

@Controller
public class BooksController {

	@Autowired
	private BooksService booksService;

	@RequestMapping("selBooks")
	@ResponseBody
	public Object selBooks(int page, int size) {
		List<Books> list = booksService.selectAllBooks(page, size);
		return list;
	}

	@RequestMapping("selBooksByWhere")
	@ResponseBody
	public Object selBooksByWhere(Books books) {
		List<Books> list = booksService.selectBooksByWhere(books);
		return list;
	}

	@RequestMapping("selBooksByWhereCatrgory")
	@ResponseBody
	public Object selBooksByWhereCatrgory() {
		List<Books> list = booksService.selectBooksByWhereCategory();
		return list;
	}

	@RequestMapping("selBooksByWhereBrand")
	@ResponseBody
	public Object selBooksByWhereBrand() {
		List<Books> list = booksService.selectBooksByWhereBrand();
		return list;
	}

	@RequestMapping("selBook")
	@ResponseBody
	public Object selBooksByPage(Books books) {
		books.setPage((books.getPage() - 1) * books.getLimit());
		int count = booksService.findCount(books);
		List<Books> list = booksService.findBooksByPage(books);
		if (list != null) {
			return Result.toClient("0", "查询成功", count, list);
		} else {
			return Result.toClient("1", "查询失败", null);
		}
	}

	@RequestMapping("updateBooks")
	@ResponseBody
	public Object updateBooksById(Books books) {
		int n = booksService.findBooks(books);
		if (n > 0) {
			return Result.toClient("0", "修改成功", n);
		} else {
			return Result.toClient("1", "修改失败", null);
		}
	}

	@RequestMapping("deleteBooks")
	@ResponseBody
	public Object deleteBooksById(int id) {
		int n = booksService.findBooksById(id);
		if (n > 0) {
			return Result.toClient("0", "删除成功", n);
		} else {
			return Result.toClient("1", "删除失败", null);
		}
	}

	@RequestMapping("addBooks")
	@ResponseBody
	public Object addBooks(Books books) {
		int n = booksService.insertBook(books);
		if (n > 0) {
			return Result.toClient("0", "添加成功", 0, null);
		} else {
			return Result.toClient("1", "添加失败", 0, null);
		}
	}
	
	@RequestMapping("updateCanUses")
	@ResponseBody
	public Object updateCanUses(Books books) {
		int n = booksService.findCanUse(books);
		if (n > 0) {
			return Result.toClient("0", "修改成功", 0, null);
		} else {
			return Result.toClient("1", "修改失败", 0, null);
		}
	}
}